<?php
/*
Primary Developer: Alex Arey
Updated: 12/10/2011
Module Description: InternshipApp provides a database query/insert 
interface and a data-object for internship applications
*/

class Application_Model_InternshipApp
{
	/**
	* Magic methods _get and _set provide access to these private data members.
	*/
	private $_appID;//application id
	private $_stuID;//student id
	private $_firstName;//student first name
	private $_midName;//student middle name
	private $_lastName;//student last name
	private $_email;//student email
	private $_phone;//student phone number (###) ###-#####
	private $_major;//student major
	private $_status;//student state/status? set as state in profile, ignored elsewhere (unclear)
	
	private $_term;//application term (spring/fall/summer)
	private $_year;//application year
	private $_gpa;//applicant gpa
	private $_transport;//applicant has transportation (y/n)
	private $_experience;//applicant experience description text
	private $_issueText;//applicant important social issue text
	private $_courseWork;//applicant relevant coursework description
	private $_resumeURL;//applicant resume url
	
	private $_suggestedpositions;//array of rows representing suggested positions

	private $_willingpositions;//array of rows representing willing positions
	
	private $_references;//array of rows representing references

	/**
	* Initialize values from DB by querying for specified application id
	* Must be called manually.
	*/
    public function init($applicationID){
		$this->_appID = $applicationID;
		$this->getApplicationData($applicationID);
		$this->getWillingPositions($applicationID);
		$this->getSuggestedPositions($applicationID);
		$this->getReferences($applicationID);
		$this->getStudentData($this->_stuID);//must be queried after applicationdata so $this->_stuID is populated
	}

	/**
	* Inserts the data from the application into the database.
	*/
	public function saveToDB(){
		//application must be saved first to provide foriegn keys for others
		$this->saveApplication();
		$this->saveReferences();
		$this->saveWillingPositions();
	}
	
	/**
	* Gets data representing the student profile for the give $studentID
	* from the Students table in the database and saves it to the class
	* data fields
	*/
	public function getStudentData($studentID){
		$dbtStu = new Application_Model_DbTable_Students();
		$student = $dbtStu->getStudent('ID = '.$studentID);
		$this->_stuID = $student['ID'];
		$this->_firstName = $student['FIRSTNAME'];
		$this->_midName = $student['MIDDLENAME'];
		$this->_lastName = $student['LASTNAME'];
		$this->_email = $student['EMAIL'];
		$this->_phone = $student['PHONE'];
		$this->_major = $student['MAJOR'];
		$this->_status = $student['STUDENTSTATE'];
	}
	
	/**
	* Retrieves data representing the application from the APPLICATIONS table in the database
	*/
	public function getApplicationData($applicationID){
		$dbtApp = new Application_Model_DbTable_Applications();
		$app = $dbtApp->getApplication($applicationID);
		$this->_stuID = $app['SID'];
		$this->_appID = $app['APPID'];
		$this->_year = $app['YEAR'];
		$this->_gpa = $app['GPA'];
		$this->_transport = $app['TRANSPORT'];
		$this->_experience = $app['EXPERIENCE'];
		$this->_issueText = $app['ISSUETEXT'];
		$this->_courseWork = $app['COURSEWORK'];
		$this->_resumeURL = $app['RESUME'];
		$this->_term = $app['TERM'];
	}
	
	/**
	* Retrieves all willing positions for the application from the WillingPositions table
	*/
	public function getWillingPositions($applicationID){
		$dbtWP = new Application_Model_DbTable_WillingPositions();
		$query = 'APPID = '.$applicationID;
		$wp = $dbtWP->getWillingPositions($query);
		$this->_willingpositions = $wp;
	}
	/**
	* Retrieves all suggested positions for the application from the ApplicationAcceptance table
	*/
	public function getSuggestedPositions($applicationID){
		$dbtSP = new Application_Model_DbTable_ApplicationAcceptance();
		$query = 'APPID = '.$applicationID;
		$sp = $dbtSP->getSuggestedPositions($query);
		$this->_suggestedpositions = $sp;
	}
	
	/**
	* Retrieves all willing positions for the application from the ApplicationReferences table
	*/
	public function getReferences($applicationID){
		$dbtRef = new Application_Model_DbTable_ApplicationReferences();
		$query = 'APPID = '.$applicationID;
		$ref = $dbtRef->getApplicationReferences($query);
		$this->_references = $ref;
	}

	/**
	* Saves application data as an insert to the Applications table
	*/
	private function saveApplication(){
		$term = new Application_Model_Term();
		$dbtApp = new Application_Model_DbTable_Applications();
		$this->_appID = $dbtApp->addApplication(
			$this->_stuID,
			$this->_year,
			$this->_gpa,
			$this->_transport,
			$this->_experience,
			$this->_issueText,
			$this->_courseWork,
			$this->_resumeURL,
			'y','y','y',//fill with 'y' because had to be yes to submit :)
			$term->getCurrentOpenTermStr('stuapp')
		);
	}
	
	/**
	* Saves all willing positions as inserts to the WillingPositions table
	*/
	private function saveWillingPositions(){
		$dbtWP = new Application_Model_DbTable_WillingPositions();
		if($this->_willingpositions){
			foreach($this->_willingpositions as $pos){
				$dbtWP->addWillingPosition(
					$this->_appID,
					$pos['POSID'],
					$pos['RANK'],
					$pos['QUALIFICATIONS']
				);
			}
		}
	}
	
	/**
	* Saves all suggested positions as inserts to the ApplicationAcceptance table
	*/
	public function saveSuggestedPositions(){
		$dbtSP = new Application_Model_DbTable_ApplicationAcceptance();
		if($this->_suggestedpositions){
			foreach($this->_suggestedpositions as $pos){
				$dbtSP->addsuggestedPosition(
					$this->_appID,
					$pos,
					null
				);
			}
		}
	}
	
	/**
	* Saves all suggested positions as inserts to the ApplicationReferences table
	*/
	private function saveReferences(){
		$dbtRef = new Application_Model_DbTable_ApplicationReferences();
		if($this->_references){
			foreach($this->_references as $ref){
				$dbtRef->addApplicationReference(
					$ref['RELATIONSHIP'],
					$this->_appID,
					$ref['NAME'],
					$ref['PHONE'],
					$ref['ADDRESS1'],
					$ref['ADDRESS2'],
					$ref['CITY'],
					$ref['STATE'],
					$ref['ZIP']
				);
			}
		}
	}
	/**
	* Removes a reference from the array in the local data object
	* Does not update database.
	* Assumes $name, $phone designates a unique contact, as it will in 99.9% of real-world cases.
	*/
	public function removeReference($name, $phone){
		foreach($this->_references as $i=>$ref){
			if($ref['NAME'] == $name && $ref['PHONE'] == $phone){
				unset($this->_references[$i]);
			}
		}
	}
	
	/**
	* Adds a reference to the local References array with given data. Does not save to database.
	*/
	public function addReference($rel,$name,$phone,$addr1,$addr2,$city,$state,$zip){
		$this->_references[] = array(
			'RELATIONSHIP'=>$rel,
			'NAME'=>$name,
			'PHONE'=>$phone,
			'ADDRESS1'=>$addr1,
			'ADDRESS2'=>$addr2,
			'CITY'=>$city,
			'STATE'=>$state,
			'ZIP'=>$zip,
		);
	}
	
	/**
	* Adds a wiling position to the local WillingPositions array with given data. Does not save to database.
	*/
	public function addWillingPosition($posid,$rank,$qualifications){
		$this->_willingpositions[] = array(
			'POSID'=>$posid,
			'RANK'=>$rank,
			'QUALIFICATIONS'=>$qualifications
		);
	}
	
	/**
	* Adds a reference to the local SuggestedPositions array with given posid. Does not save to database.
	*/
	public function addSuggestedPosition($posid){
		$this->_suggestedpositions[] = $posid;
	}
	
	/**
	* Returns the acceptance status for the application and the given position.
	* Acceptance statuses are defined as follows:
	*	null=>submitted and forwarded to partner, but no decision made
	*	'c'=>community partner has accepted but no decision from student
	*	'd'=>community partner has denied the application
	*	's'=>student has accepted the position offer
	*	't'=>student has turned down the position offer
	*/
    public function getStatus_Pos($posid){
    	$iq = new Application_Model_DbTable_ApplicationAcceptance();
    	$application = $iq->getSuggestedPosition('APPID = '.$this->_appID.' and POSID = '.$posid);
    	return $application['ACCEPTED'];
    }
	
	/**
	* Returns a string representing the highest acceptance status for the application.
	* Acceptance statuses are defined as follows:
	*	null=>submitted and forwarded to partner, but no decision made, returns 'No Decision'
	*	'c'=>community partner has accepted but no decision from student, returns 'Accepted by Partner'
	*	'd'=>community partner has denied the application, returns 'Partner Turned Down Applicant'
	*	's'=>student has accepted the position offer, returns 'Student Accepted Offer'
	*	't'=>student has turned down the position offer, returns 'Student Turned Down Offer'
	* Order determining value to return defined d < t < null < c < s
	*/
    public function getHighestAcceptance(){
    	$iq = new Application_Model_DbTable_ApplicationAcceptance();
    	$positions = $iq->getSuggestedPositions('APPID = '.$this->_appID);
    	$bestpos = '';
    	foreach($positions as $p){
    		if($p['ACCEPTED']=='s')$bestpos = 's';
    		if($p['ACCEPTED']=='c'&& $bestpos!='s')$bestpos = 'c';
    		if($p['ACCEPTED']=='n'&& ($bestpos != 's' && $bestpos != 'c'))$bestpos = 'n';
    		if($p['ACCEPTED']=='t' && ($bestpos == '' || $bestpos=='d'))$bestpos = 't';
    		if($p['ACCEPTED']=='d' && $bestpos == '')$bestpos = 'd';
    	}
    	if($bestpos=='n' || $bestpos == '') return 'No Decision Made';
    	if($bestpos=='c') return 'Accepted by Partner';
    	if($bestpos=='s') return 'Student Accepted Offer';
    	if($bestpos=='d') return 'Partner Turned Down Applicant';
    	if($bestpos=='t') return 'Student Turned Down Offer';
    }
	
	
	/**
	* Magic method getter/setter functions for all private data objects
	*/
	public function __set($name, $value){
        $this->$name = $value;
    }
 
    public function __get($name){
		return $this->$name;
    }

}
?>
